/**
 *
 */
package com.ybkj.daijia.server.service.impl;


import com.ybkj.daijia.common.Pager;
import com.ybkj.daijia.common.Principal;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.page.Pageable;
import com.ybkj.daijia.page.impl.PageImpl;
import com.ybkj.daijia.page.impl.PageRequest;
import com.ybkj.daijia.server.mapper.WeixinManagerMapper;
import com.ybkj.daijia.server.model.WeixinManager;
import com.ybkj.daijia.server.service.WeixinManagerService;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Shine
 *
 */
@Service
public class DefaultWeixinManagerService implements WeixinManagerService {

    @Autowired
    private WeixinManagerMapper weixinManagerMapper;

    /* (non-Javadoc)
     * @see com.ybkj.shop.service.AdminService#queryAll()
     */
    public Page<WeixinManager> queryAll(String phone, String name, String placeName, String idCard,
        Long companyId, Integer virtualType) {

        Subject currentUser = SecurityUtils.getSubject();
        Principal principal = (Principal) currentUser.getPrincipal();

        Pageable pageable = new PageRequest(Pager.getPage(), Pager.getLimit());

        if (!principal.isCompanyType()) {
            companyId = principal.getCompanyId();
        }

        if (StringUtils.isNotBlank(phone)) {
            phone = "%" + phone + "%";
        } else {
            phone = null;
        }

        if (StringUtils.isNotBlank(name)) {
            name = "%" + name + "%";
        } else {
            name = null;
        }

        if (StringUtils.isNotBlank(placeName)) {
            placeName = "%" + placeName + "%";
        } else {
            placeName = null;
        }

        if (StringUtils.isNotBlank(idCard)) {
            idCard = "%" + idCard + "%";
        } else {
            idCard = null;
        }

        if (null != virtualType) {
            if (!virtualType.equals(1) && !virtualType.equals(2)) {
                virtualType = null;
            }
        }

        List<WeixinManager> content = weixinManagerMapper
            .selectByPage(companyId, phone, name, placeName, idCard, virtualType, Pager.getOffset(),
                Pager.getLimit());
        long total = weixinManagerMapper
            .countByPage(companyId, phone, name, placeName, idCard, virtualType);

        return new PageImpl<WeixinManager>(content, pageable, total);

    }

    @Transactional(rollbackFor = Exception.class)
    public WeixinManager insertRecord(WeixinManager weixinManager) {

        weixinManagerMapper.insertRecord(weixinManager);

        return weixinManager;
    }

    public WeixinManager findOne(Long id) {

        return weixinManagerMapper.selectByPrimaryKey(id);
    }

    @Override
    public WeixinManager findByWxuser(String wxuser) {

        return weixinManagerMapper.selectByWxuser(wxuser);
    }

    @Override
    public WeixinManager findByPhone(String phone) {

        return weixinManagerMapper.selectByPhone(phone);
    }

    @Override
    public WeixinManager findByIdCard(String idCard) {

        return weixinManagerMapper.selectByIdCard(idCard);
    }

    @Transactional(rollbackFor = Exception.class)
    public void delete(WeixinManager weixinManager) {

        weixinManagerMapper.deleteByPrimaryKey(weixinManager.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateByPrimaryKey(WeixinManager weixinManager) {

        return weixinManagerMapper.updateByPrimaryKey(weixinManager);
    }

    @Override
    public List<WeixinManager> findByCompany(Long companyId) {

        return weixinManagerMapper.selectByCompany(companyId);
    }

    @Override
    public List<WeixinManager> findOKByCompany(Long companyId) {

        return weixinManagerMapper.selectOKByCompany(companyId);

    }

}